{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import time as tm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始执行，请等待。。。。\n"
     ]
    }
   ],
   "source": [
    "print('开始执行，请等待。。。。')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "打开MCU1-0的sheet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd_Signal_sheet = pd.read_excel(\"./SignalGroup_CanMsgSaf.xlsx\", 'PAS4-MCU1_0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd_Signal_Group = pd_Signal_sheet['SigGroup']\n",
    "pd_Signal_Name = pd_Signal_sheet['EnumName']\n",
    "pd_Signal_ErrorFactor = pd_Signal_sheet['error_factor']\n",
    "pd_Signal_CRCcheck = pd_Signal_sheet['CRC check']\n",
    "pd_Signal_CounterCheck = pd_Signal_sheet['counter check']\n",
    "pd_Signal_QFCheck = pd_Signal_sheet['QF check']\n",
    "pd_Signal_QfName = pd_Signal_sheet['QF signal']\n",
    "pd_Signal_QFvalue = pd_Signal_sheet['QF value']\n",
    "pd_Signal_Port = pd_Signal_sheet['Port']\n",
    "pd_Signal_SWRS = pd_Signal_sheet['SWRS']\n",
    "pd_Signal_SWRS_ID = pd_Signal_sheet['SWRS ID']\n",
    "pd_Signal_ErrorID = pd_Signal_sheet['Error ID']\n",
    "pd_Signal_Period = pd_Signal_sheet['period time(10ms)']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd_Signal_RD_sheet = pd.read_excel(\"./SignalGroup_CanMsgSaf.xlsx\", 'PAS4-MCU3_0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "port_name = \"Rte_Read_\"\n",
    "port_name_list = []\n",
    "var_name = 'CanMsgSaf_'\n",
    "var_name_list = []\n",
    "fun_name = 'Appl_CanMsgSaf_'\n",
    "fun_name_list = []\n",
    "fun_return_type = 'CanMsgSaf_PRI void '\n",
    "fun_input_type = '()'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "for (Port,signame) in zip(pd_Signal_Port,pd_Signal_Group):\n",
    "    if Port != 'END':\n",
    "        port_name_list.append(port_name + Port + '_SigGroup_' +signame)\n",
    "    else:\n",
    "        break\n",
    "for group in pd_Signal_Group:\n",
    "    if group != 'END':\n",
    "        var_name_list.append(var_name + group)\n",
    "    else:\n",
    "        break \n",
    "for group in pd_Signal_Group:\n",
    "    if group != 'END':\n",
    "        fun_name_list.append(fun_return_type + fun_name + group + fun_input_type)\n",
    "    else:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_file_head(file_path):\n",
    "    print(\"/***********************************************************************\", file=file_path)\n",
    "    print(\"Department : IDA-SW2\", file=file_path)\n",
    "    print(\"Time :\" + tm.strftime('%Y-%m-%d %H:%M:%S',tm.localtime(tm.time())) , file=file_path)\n",
    "    print(\"************************************************************************\", file=file_path)\n",
    "    print(\"* Object        : Appl_CanMsgSaf.c\",file=file_path)\n",
    "    print(\"* Module        : CanMsgSaf SWC\",file=file_path)\n",
    "    print(\"* Instance      : \",file=file_path)\n",
    "    print(\"* Description   : Can msg function safety\",file=file_path) \n",
    "    print(\"*-----------------------------------------------------------------------*/\",file=file_path)\n",
    "    print(\"\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_file_Includes(file_path):\n",
    "    print(\"/*=====================Includes=======================================*/\",file=file_path)\n",
    "    print(\"#include \\\"Appl_CanMsgSaf.h\\\" \", file=file_path)\n",
    "    print(\"#include \\\"Rte_CtApCanMsgSaf.h\\\" \", file=file_path)\n",
    "    print(\"#include \\\"app_if.h\\\" \", file=file_path)\n",
    "    print(\"#include \\\"string.h\\\" \", file=file_path)\n",
    "    print(\"\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_file_Macros(file_path):\n",
    "    print(\"/*=====================Macros=========================================*/\\n\",file=file_path)\n",
    "    print(\"#define CanMsgSaf_NoError             0x00\",file=file_path)\n",
    "    print(\"#define CanMsgSaf_HaveError           0x01\",file=file_path)\n",
    "    print(\"# define STD_OFF                      0u\",file=file_path)\n",
    "    print(\"# define STD_ON                       1u\",file=file_path)\n",
    "    print(\"#define CanMsgSaf_DEBUG               STD_OFF\",file=file_path)\n",
    "    print(\"\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_file_Variables(file_path):\n",
    "    print(\"/*=====================Local Variables================================*/\",file=file_path)\n",
    "    print(\"enum{\",file=file_path)\n",
    "    print(\"\\t\" + \"NORMAL = 0x00,\",file=file_path)\t\n",
    "    print(\"\\t\" + \"VALUE_ERROR = 0x01,\",file=file_path)\n",
    "    print(\"\\t\" + \"VALUE_OUTOFRANGE = 0x02,\",file=file_path)\n",
    "    print(\"\\t\" + \"RESERVE_00 = 0x04,\",file=file_path)\n",
    "    print(\"\\t\" + \"COMM_ERROR = 0x08,\",file=file_path)\n",
    "    print(\"\\t\" + \"RESERVE_01 = 0x10,\",file=file_path)\n",
    "    print(\"};\\n\",file=file_path)\n",
    "    print(\"CanMsgSaf_SigGroupInfo_t CanMsgSaf_SigGroupInfo[ SigGroupAll ];\\n\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_function_describe(fun_name, signal_group_name, swrs_name, swrs_id,  file_path):\n",
    "    print(\"/***********************************************************************\",file=file_path)\n",
    "    print(\"* FUNCTION    :\" + fun_name.replace('CanMsgSaf_PRI',''), file=file_path)\n",
    "    print(\"* DESCRIPTION :\" + \" SigGroup\" + signal_group_name +': ' + swrs_name ,file=file_path)\n",
    "    print(\"* SWRS-ID     : \" + swrs_id.replace('\\n',','), file=file_path)\n",
    "    print(\"* INPUTS      : From RTE\", file=file_path)\n",
    "    print(\"* RETURN      : None\", file=file_path)\n",
    "    print(\"***********************************************************************/\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_function_var(signal_group_name, file_path):\n",
    "    print('\\t' + 'unsigned char CanMsgSaf_recover = CanMsgSaf_NoError;',file=file_path)\n",
    "    print(\"\",file=file_path)\n",
    "    print('\\t' + 'Idt' + signal_group_name + ' ' + signal_group_name.replace('_','') + ';',file=file_path)\n",
    "    print(\"\\n\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_function_tout(signal_group_name,E_group_name,signal_timeout,file_path):\n",
    "    signal_group_name = signal_group_name.replace('_','')\n",
    "    print(\"\\t\" + '/* Determine whether the message time out  */', file=file_path)\n",
    "    print('\\t',end = '',file=file_path)\n",
    "    print('if(', end = '',file=file_path)\n",
    "    print('((' + signal_group_name + '.E2eP01Sta.Status' + '&' + 'E2eP01Status_NoNewData)' + '==' + 'E2eP01Status_NoNewData' + ')\\\\\\n'\n",
    "          '\\t\\t\\t\\t\\t'+ '&& ' + \"(CanMsgSaf_recover != CanMsgSaf_HaveError))\",file=file_path)\n",
    "    print(\"\\t\" + \"{\", file=file_path)\n",
    "    print(\"\\t\\t\" + \"if( CanMsgSaf_SigGroupInfo[\"+ E_group_name + \"].CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_Tcount < \" + str(signal_timeout) + \" )\", file=file_path)\n",
    "    print(\"\\t\\t\" + \"{\", file=file_path)\n",
    "    print(\"\\t\\t\\t\" + \"CanMsgSaf_SigGroupInfo[\"+ E_group_name + \"].CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_Tcount++;\", file=file_path)\n",
    "    print(\"\\t\\t\" + \"}\", file=file_path)\n",
    "    print(\"\\t\\t\" + \"else\", file=file_path)\n",
    "    print(\"\\t\\t\" + \"{\", file=file_path)\n",
    "    print(\"\\t\\t\\t\\t\" + \"CanMsgSaf_recover =  CanMsgSaf_HaveError; \", file=file_path)\n",
    "    print(\"\\t\\t\\t\" + '#if (CanMsgSaf_DEBUG == STD_ON)', file=file_path)\n",
    "    print(\"\\t\\t\\t\\t\" + \"appLogPrintf(\" +\"\\\"\" + signal_group_name.replace('CanMsgSaf', '') + ' time out! ' +'\\\\n' + '\\\"' + \");\", file=file_path)\n",
    "    print(\"\\t\\t\\t\" + '#endif', file=file_path)\n",
    "    print(\"\\t\\t\" + \"}\", file=file_path)\n",
    "    print(\"\\t\" + \"}\", file=file_path)\n",
    "    print(\"\\t\" + \"else\", file=file_path)\n",
    "    print(\"\\t\" + \"{\", file=file_path)\n",
    "    print(\"\\t\\t\" + \"CanMsgSaf_SigGroupInfo[\"+ E_group_name + \"].CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_Tcount = 0;\", file=file_path)\n",
    "    print(\"\\t\" + \"}\", file=file_path)\n",
    "    print(\"\\n\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_function_crc(crc_ck,signal_group_name,file_path):\n",
    "    if(crc_ck == True):\n",
    "        signal_group_name = signal_group_name.replace('_','')\n",
    "        print(\"\\t\" + '/* Determine whether the message crc error */', file=file_path)\n",
    "        print('\\t',end = '',file=file_path)\n",
    "        print('if(', end = '',file=file_path)\n",
    "        print('((' + signal_group_name + '.E2eP01Sta.Status' + '&' + 'E2eP01Status_WrongCrc)' + '==' + 'E2eP01Status_WrongCrc' + ')\\\\\\n'\n",
    "              '\\t\\t\\t\\t\\t'+ '&& '+ \"(CanMsgSaf_recover != CanMsgSaf_HaveError))\",file=file_path)\n",
    "        print(\"\\t\" + \"{\", file=file_path)\n",
    "        print(\"\\t\\t\" + \"CanMsgSaf_recover =  CanMsgSaf_HaveError; \", file=file_path)\n",
    "        print(\"\\t\" + '#if (CanMsgSaf_DEBUG == STD_ON)', file=file_path)\n",
    "        print(\"\\t\\t\" + \"appLogPrintf(\" +\"\\\"\" + signal_group_name.replace('CanMsgSaf', '') + ' crc error ' +'\\\\n' + '\\\"' + \");\", file=file_path)\n",
    "        print(\"\\t\" + '#endif', file=file_path)\n",
    "        print(\"\\t\" + \"}\", file=file_path)\n",
    "        print(\"\\n\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_function_counter(count_ck,signal_group_name,file_path):\n",
    "    if(count_ck == True):\n",
    "        signal_group_name = signal_group_name.replace('_','')\n",
    "        print(\"\\t\" + '/* Determine whether the message count error */', file=file_path)\n",
    "        print('\\t',end = '',file=file_path)\n",
    "        print('if(', end = '',file=file_path)\n",
    "        print('((' + signal_group_name + '.E2eP01Sta.Status' + '&' + 'E2eP01Status_Repeated)' + '==' + 'E2eP01Status_Repeated' + ')\\\\\\n'\n",
    "              '\\t\\t\\t\\t\\t'+ '&& '+ \"(CanMsgSaf_recover != CanMsgSaf_HaveError))\",file=file_path)\n",
    "        print(\"\\t\" + \"{\", file=file_path)\n",
    "        print(\"\\t\\t\" + \"CanMsgSaf_recover =  CanMsgSaf_HaveError; \", file=file_path)\n",
    "        print(\"\\t\" + '#if (CanMsgSaf_DEBUG == STD_ON)', file=file_path)\n",
    "        print(\"\\t\\t\" + \"appLogPrintf(\" +\"\\\"\" + signal_group_name.replace('CanMsgSaf', '') + ' count error ' +'\\\\n' + '\\\"' + \");\", file=file_path)\n",
    "        print(\"\\t\" + '#endif', file=file_path)\n",
    "        print(\"\\t\" + \"}\", file=file_path)\n",
    "        print(\"\\n\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_function_qf(qf_check, signal_group_name, qf_lsit, qf_value, file_path):\n",
    "    signal_group_name_use = signal_group_name.replace('_','')\n",
    "    if(qf_check == True):\n",
    "        for qf in qf_lsit.split(','):\n",
    "            print(\"\\t\" + '/* Determine whether the message Qf value error */', file=file_path) \n",
    "            print(\"\\t\" +'if(', end = '',file=file_path)\n",
    "            print('(' + signal_group_name_use + '.' + qf + ' != ' + str(qf_value) + ')' +'\\\\\\n' +'\\t\\t\\t\\t\\t&& ' + \"(CanMsgSaf_recover != CanMsgSaf_HaveError)\" + ')', file=file_path)\n",
    "            print(\"\\t\" + \"{\", file=file_path)\n",
    "            print(\"\\t\\t\" + \"CanMsgSaf_recover = CanMsgSaf_HaveError; \", file=file_path)\n",
    "            print(\"\\t\" + '#if (CanMsgSaf_DEBUG == STD_ON)', file=file_path)\n",
    "            print(\"\\t\\t\" + \"appLogPrintf(\" +\"\\\"\" + qf + ' error ' +'\\\\n' + '\\\"' + \");\", file=file_path)\n",
    "            print(\"\\t\" + '#endif', file=file_path)\n",
    "            print(\"\\t\" + \"}\", file=file_path)\n",
    "            print(\" \", file=file_path)\n",
    "    else:\n",
    "        print(\"\\n\\t/********************There is no QF*********************************/\\n\", file=file_path)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_function_clean(signal_group_name,E_group_name,file_path):\n",
    "    signal_group_name = signal_group_name.replace('CanMsgSaf_','')\n",
    "    print(\"\\t\" + '/* error clean */', file=file_path)\n",
    "    print(\"\\t\" + 'if( CanMsgSaf_recover !=  CanMsgSaf_HaveError )', file=file_path)\n",
    "    print(\"\\t\" + \"{\", file=file_path) \n",
    "    print(\"\\t\\t\" + 'CanMsgSaf_SigGroupInfo['+ E_group_name + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_count = 0;', file=file_path)\n",
    "    print(\"\\t\\t\" + 'CanMsgSaf_SigGroupInfo['+ E_group_name + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_flag = 0;', file=file_path)\n",
    "    print(\"\\t\\t\" + '#if (CanMsgSaf_DEBUG == STD_ON)', file=file_path)\n",
    "    print(\"\\t\\t\" + \"appLogPrintf(\" +\"\\\"\" + signal_group_name + ' reception OK! ' +'\\\\n' + '\\\"' + \");\", file=file_path)\n",
    "    print(\"\\t\\t\" + '#endif', file=file_path)\n",
    "    print(\"\\t\" + \"}\", file=file_path)\n",
    "    print(\"\\t\" + 'else', file=file_path)\n",
    "    print(\"\\t\" + \"{\", file=file_path)\n",
    "    print(\"\\t\\t\" + 'if(CanMsgSaf_SigGroupInfo['+ E_group_name + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_count  < 0x7f)', file=file_path)\n",
    "    print(\"\\t\\t\" + \"{\", file=file_path)\n",
    "    print(\"\\t\\t\\t\" + 'CanMsgSaf_SigGroupInfo['+ E_group_name + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_count++;', file=file_path)\n",
    "    print(\"\\t\\t\" + '#if (CanMsgSaf_DEBUG == STD_ON)', file=file_path)\n",
    "    print(\"\\t\\t\\t\" + 'if(CanMsgSaf_SigGroupInfo['+ E_group_name + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_count  <= 0x03)', file=file_path)\n",
    "    print(\"\\t\\t\\t\" + \"{\", file=file_path)\n",
    "    print(\"\\t\\t\\t\\t\" + \"appLogPrintf(\" + \"\\\"\"+'CanMsgSaf_SigGroupInfo['+ E_group_name + ']'\n",
    "    +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_count : %d'  +'\\\\n' + '\\\"'+ \",CanMsgSaf_SigGroupInfo[\" + E_group_name + ']' + '.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_count' \");\"\n",
    "    , file=file_path)\n",
    "    print(\"\\t\\t\\t\" + \"}\", file=file_path)\n",
    "    print(\"\\t\\t\" + '#endif', file=file_path)\n",
    "    print(\"\\t\\t\" + \"}\", file=file_path)\n",
    "    print(\"\\t\" + \"}\\n\", file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_function_entry_safety(signal_group_name,E_group_name,error_id,error_value,file_path):\n",
    "    signal_group_name = signal_group_name.replace('CanMsgSaf_','')\n",
    "    print(\"\\t\" + '/* Whether to enter functional safety */', file=file_path)\n",
    "    #-----------------------------------------------------------------------------------------------------\n",
    "    #-----------------------------------------------------------------------------------------------------\n",
    "    print(\"\\t\" + 'if(' +  'CanMsgSaf_SigGroupInfo['+ E_group_name + '].CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_count  >= '+ str(error_value) + ' )', file=file_path)\n",
    "    print(\"\\t\" + \"{\", file=file_path)\n",
    "    print(\"\\t\\t\" + 'CanMsgSaf_SigGroupInfo['+ E_group_name + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_flag = 1;', file=file_path)\n",
    "    #-----------------------------------------------------------------------------------------------------\n",
    "    #-----------------------------------------------------------------------------------------------------\n",
    "    print(\"\\t\" + '#if (CanMsgSaf_DEBUG == STD_ON)', file=file_path)\n",
    "    print(\"\\t\\t\" + \"appLogPrintf(\" +\"\\\"\" + signal_group_name + ' CanMsgSaf_flag = 1' +'\\\\n' + '\\\"' + \");\", file=file_path)\n",
    "    print(\"\\t\" + '#endif', file=file_path)\n",
    "    if(str(error_id) != 'nan'):\n",
    "        print(\"\\t\\t\" + 'Rte_Call_ErrorIdStatus_SetErrorIdStatus('+ str(error_id) + ',COMM_ERROR' +');', file=file_path)\n",
    "    #-----------------------------------------------------------------------------------------------------\n",
    "    #-----------------------------------------------------------------------------------------------------\n",
    "    print(\"\\t\" + \"}\", file=file_path)\n",
    "    print(\"\\t\" + \"else\", file=file_path)\n",
    "    print(\"\\t\" + \"{\", file=file_path)\n",
    "    if(str(error_id) != 'nan'):\n",
    "        print(\"\\t\\t\" + 'Rte_Call_ErrorIdStatus_SetErrorIdStatus('+ str(error_id) + ',NORMAL' +');', file=file_path)\n",
    "    print(\"\\t\" + \"}\", file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_function_Process(file_path):\n",
    "    print(\"/***********************************************************************\",file=file_path)\n",
    "    print(\"* FUNCTION    :\" + 'void ' + \"Appl_CanMsgSafProcess\", file=file_path)\n",
    "    print(\"* DESCRIPTION :\" + \"Appl_CanMsgSafProcess\"  ,file=file_path)\n",
    "    print(\"* INPUTS      : None\", file=file_path)\n",
    "    print(\"* RETURN      : None\", file=file_path)\n",
    "    print(\"***********************************************************************/\",file=file_path)\n",
    "    print( 'void ' + \"Appl_CanMsgSafProcess(void)\", file=file_path)\n",
    "    print('{',file=file_path)\n",
    "    for f_name in fun_name_list:\n",
    "        print('\\t'+ f_name.replace(' ', '').replace('CanMsgSaf_PRI', '').replace('void', '') +';' ,file=file_path)\n",
    "    print('}',file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_function_GetStatus(file_path):\n",
    "    print(\"/***********************************************************************\",file=file_path)\n",
    "    print(\"* FUNCTION    :\" + 'void ' + \"Appl_CanMsgSafGetStatus\", file=file_path)\n",
    "    print(\"* DESCRIPTION :\" + \"Appl_CanMsgSafGetStatus\"  ,file=file_path)\n",
    "    print(\"* INPUTS      : None\", file=file_path)\n",
    "    print(\"* RETURN      : None\", file=file_path)\n",
    "    print(\"***********************************************************************/\",file=file_path)\n",
    "    print( 'unsigned char Appl_CanMsgSafGetStatus(CanMsgSaf_SignalName_t CanMsgSaf_SignalName)', file=file_path)\n",
    "    print('{',file=file_path)\n",
    "    print('\\t'+ 'unsigned char CanMsgSafFalg = 0;\\n' ,file=file_path)\n",
    "    print('\\t'+ 'if(CanMsgSaf_SignalName <  SigGroupAll)' ,file=file_path)\n",
    "    print('\\t{',file=file_path)\n",
    "    print('\\t\\t'+ 'CanMsgSafFalg =  CanMsgSaf_SigGroupInfo[CanMsgSaf_SignalName].CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_flag;' ,file=file_path) \n",
    "    print('\\t}',file=file_path)\n",
    "    print('\\t' + 'else',file=file_path)\n",
    "    print('\\t{',file=file_path)\n",
    "    print('\\t\\t' + 'CanMsgSafFalg =  0xff;',file=file_path)\n",
    "    print('\\t}',file=file_path)\n",
    "    print('\\t' +'return CanMsgSafFalg;',file=file_path)\n",
    "    print('}',file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_function_Init(file_path):\n",
    "    print(\"/***********************************************************************\",file=file_path)\n",
    "    print(\"* FUNCTION    :\" + 'void ' + \"Appl_CanMsgSafInit\", file=file_path)\n",
    "    print(\"* DESCRIPTION :\" + \"Appl_CanMsgSafInit\"  ,file=file_path)\n",
    "    print(\"* INPUTS      : None\", file=file_path)\n",
    "    print(\"* RETURN      : None\", file=file_path)\n",
    "    print(\"***********************************************************************/\",file=file_path)\n",
    "    print( 'void ' + \"Appl_CanMsgSafInit(void)\", file=file_path)\n",
    "    print('{',file=file_path)\n",
    "    print('\\t' +'memset(&CanMsgSaf_SigGroupInfo,0,sizeof(CanMsgSaf_SigGroupInfo));',file=file_path)\n",
    "    print('}',file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gen_c_file():\n",
    "    i = 0\n",
    "    with open('./Appl_CanMsgSaf.c',mode='w', encoding='UTF-8') as file_path:\n",
    "        define_file_head(file_path)\n",
    "        define_file_Includes(file_path)\n",
    "        define_file_Macros(file_path)\n",
    "        define_file_Variables(file_path)\n",
    "        for fun_name in fun_name_list:\n",
    "            define_function_describe(fun_name,pd_Signal_Group.at[i],pd_Signal_SWRS.at[i].replace('\\n',' '),pd_Signal_SWRS_ID.at[i].replace('\\n', ' '),file_path)\n",
    "            print(fun_name,file = file_path)\n",
    "            print('{',file = file_path)\n",
    "            if((pd_Signal_CRCcheck.at[i] == \"FLASE\") and (pd_Signal_CounterCheck.at[i] == 'FLASE')):\n",
    "                print(\"\\t\" + 'uint8 '  + pd_Signal_Group.at[i] + '= CanMsgSaf_NoError;', file=file_path)\n",
    "                print(\"\\t\" + 'Rte_Read_' + pd_Signal_Port.at[i] +'_Sig_'+ pd_Signal_Group.at[i] + '(&'+ pd_Signal_Group.at[i] + ');', file=file_path)\n",
    "                print(\"\\t\" + 'if(' + pd_Signal_Group.at[i] + ' == CanMsgSaf_HaveError' +')', file=file_path)\n",
    "                print(\"\\t\" + \"{\", file=file_path)\n",
    "                print(\"\\t\\t\" + 'if(CanMsgSaf_SigGroupInfo['+ pd_Signal_Name.at[i] + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_count  < 0x7f)', file=file_path)\n",
    "                print(\"\\t\\t\" + \"{\", file=file_path)\n",
    "                print(\"\\t\\t\\t\" + 'CanMsgSaf_SigGroupInfo['+ pd_Signal_Name.at[i] + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_count++;', file=file_path)\n",
    "                print(\"\\t\\t\" + \"}\", file=file_path)\n",
    "                print(\"\\t\" + \"}\", file=file_path)\n",
    "                print(\"\\t\" + \"else\", file=file_path)\n",
    "                print(\"\\t\" + \"{\", file=file_path)\n",
    "                print(\"\\t\\t\" + 'CanMsgSaf_SigGroupInfo['+ pd_Signal_Name.at[i] + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_count = 0;', file=file_path)\n",
    "                print(\"\\t\\t\" + 'CanMsgSaf_SigGroupInfo['+ pd_Signal_Name.at[i] + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_flag = 0;', file=file_path)\n",
    "                print(\"\\t\" + \"}\\n\", file=file_path)\n",
    "                print(\"\\t\" + 'if(' +  'CanMsgSaf_SigGroupInfo['+ pd_Signal_Name.at[i] + '].CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_count  >= '+ str(pd_Signal_ErrorFactor.at[i]) + ' )', file=file_path)\n",
    "                print(\"\\t\" + \"{\", file=file_path)\n",
    "                print(\"\\t\\t\" + 'CanMsgSaf_SigGroupInfo['+ pd_Signal_Name.at[i] + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_flag = 1;', file=file_path)\n",
    "                print(\"\\t\" + '#if (CanMsgSaf_DEBUG == STD_ON)', file=file_path)\n",
    "                print(\"\\t\\t\" + \"appLogPrintf(\" +\"\\\"\" + 'CanMsgSaf_SigGroupInfo['+ pd_Signal_Name.at[i] + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_flag = %d' +'\\\\n' + '\\\"' + ' \\\\\\n\\t\\t\\t\\t\\t'\n",
    "                      ', CanMsgSaf_SigGroupInfo['+ pd_Signal_Name.at[i] + ']' +'.CanMsgSaf_SigGroupInfo_E2E.CanMsgSaf_flag'+ \");\", file=file_path)\n",
    "                print(\"\\t\" + '#endif', file=file_path)\n",
    "                if(pd.isnull(pd_Signal_ErrorID.at[i]) != True):\n",
    "                    print(\"\\t\\t\" + 'Rte_Call_ErrorIdStatus_SetErrorIdStatus(' + str(pd_Signal_ErrorID.at[i]) + ',COMM_ERROR' + ');', file=file_path)\n",
    "                print('\\t}',file=file_path)\n",
    "                print(\"\\t\" + \"else\", file=file_path)\n",
    "                print(\"\\t\" + \"{\", file=file_path)\n",
    "                if(pd.isnull(pd_Signal_ErrorID.at[i]) != True):\n",
    "                    print(\"\\t\\t\" + 'Rte_Call_ErrorIdStatus_SetErrorIdStatus(' + str(pd_Signal_ErrorID.at[i]) + ',NORMAL' + ');', file=file_path)\n",
    "                print(\"\\t\" + \"}\", file=file_path)\n",
    "                print('}',file = file_path)\n",
    "            else:\n",
    "                define_function_var(var_name_list[i] ,file_path)\n",
    "                print('\\t' + '/* read msg of ' + var_name_list[i].replace('CanMsgSaf_','') + ' from E2E SWC */', file = file_path)\n",
    "                print('\\t' + port_name_list[i] + '(' + ' &' + var_name_list[i].replace('_','') + ' );' ,file = file_path)\n",
    "                define_function_tout(var_name_list[i],pd_Signal_Name.at[i],pd_Signal_Period.at[i] ,file_path)\n",
    "                define_function_crc(pd_Signal_CRCcheck.at[i],var_name_list[i] ,file_path)\n",
    "                define_function_counter(pd_Signal_CounterCheck.at[i],var_name_list[i],file_path)\n",
    "                define_function_qf(pd_Signal_QFCheck.at[i],var_name_list[i],pd_Signal_QfName.at[i], pd_Signal_QFvalue.at[i],file_path)\n",
    "                define_function_clean(var_name_list[i],pd_Signal_Name.at[i],file_path)\n",
    "                define_function_entry_safety(var_name_list[i],pd_Signal_Name.at[i],pd_Signal_ErrorID.at[i],pd_Signal_ErrorFactor.at[i],file_path)\n",
    "                print('}',file = file_path)\n",
    "                print(\"\\n\",file=file_path)\n",
    "            i = i + 1\n",
    "        i = 0;\n",
    "        define_function_Process(file_path)\n",
    "        define_function_GetStatus(file_path)\n",
    "        define_function_Init(file_path)\n",
    "    file_path.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gen_h_file():\n",
    "    with open('./Appl_CanMsgSaf.h',mode='w') as file_path:\n",
    "        print(\"/***********************************************************************\", file=file_path)\n",
    "        print(\"Department : IDA-SW2\", file=file_path)\n",
    "        print(\"Time :\" + tm.strftime('%Y-%m-%d %H:%M:%S',tm.localtime(tm.time())) , file=file_path)\n",
    "        print(\"************************************************************************\", file=file_path)\n",
    "        print(\"* Object        : Appl_CanMsgSaf.h\",file=file_path)\n",
    "        print(\"* Module        : CanMsgSaf SWC\",file=file_path)\n",
    "        print(\"* Instance      : \",file=file_path)\n",
    "        print(\"* Description   : Can msg function safety\",file=file_path) \n",
    "        print(\"*-----------------------------------------------------------------------*/\",file=file_path)\n",
    "        print(\"#ifndef Appl_CanMsgSaf_H\",file=file_path)\n",
    "        print(\"#define Appl_CanMsgSaf_H\",file=file_path)\n",
    "        print(\"/*=====================Includes=======================================*/\",file=file_path)\n",
    "        print(\"\",file=file_path)\n",
    "        print(\"/*=====================Macros=========================================*/\",file=file_path)\n",
    "        print(\"#define CanMsgSaf_PRI static\",file=file_path)\n",
    "        print(\"/*=====================Local Variables================================*/\",file=file_path)\n",
    "        print(\"typedef struct {\",file=file_path)\n",
    "        print(\"\\t unsigned char CanMsgSaf_flag:1;\",file=file_path)\n",
    "        print(\"\\t unsigned char CanMsgSaf_count:7;\",file=file_path)\n",
    "        print(\"\\t unsigned char CanMsgSaf_Tcount;\",file=file_path)\n",
    "        print(\"}CanMsgSaf_SigGroupInfo_E2E_t;\\n\",file=file_path)\n",
    "\n",
    "        print(\"typedef void (*CanMsgSaf_CanMsgSafEntry_t)(void);\\n\",file=file_path)\n",
    "\n",
    "\n",
    "        print(\"typedef struct {\",file=file_path)\n",
    "        print(\"\\t CanMsgSaf_SigGroupInfo_E2E_t CanMsgSaf_SigGroupInfo_E2E;\",file=file_path)\n",
    "        print(\"\\t CanMsgSaf_CanMsgSafEntry_t CanMsgSaf_CanMsgSafEntry;\",file=file_path)\n",
    "        print(\"}CanMsgSaf_SigGroupInfo_t;\\n\",file=file_path)\n",
    "\n",
    "        i = 0\n",
    "        print(\"typedef enum {\", file=file_path)\n",
    "        while(pd_Signal_Name.at[i] != 'END'):\n",
    "            print('\\t' + pd_Signal_Name.at[i] + \" = \" + str(i) +',', file=file_path)\n",
    "            i = i + 1\n",
    "        print(\"\\tSigGroupAll\", file=file_path) \n",
    "        print(\"}CanMsgSaf_SignalName_t;\\n\", file=file_path) \n",
    "        i = 0\n",
    "\n",
    "        # for f_name in function_name_extern:\n",
    "        #     print(\"extern void \" + f_name +';' ,file=file_path)\n",
    "        print( 'extern void ' + \"Appl_CanMsgSaf_Init(void);\", file=file_path)  \n",
    "        print( 'extern void ' + \"Appl_CanMsgSafProcess(void);\", file=file_path)\n",
    "        print( 'extern unsigned char ' + \"Appl_CanMsgSafGetStatus(CanMsgSaf_SignalName_t CanMsgSaf_SignalName);\", file=file_path)\n",
    "    #     print( 'extern void ' + \"Appl_CanMsgSafEntry(void);\", file=file_path)\n",
    "        print( '\\n\\n\\n', file=file_path)\n",
    "        print( '#endif /* Appl_CanMsgSaf_H */', file=file_path)\n",
    "        file_path.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "gen_c_file()\n",
    "gen_h_file()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "Rd_SigGrop_list = pd_Signal_RD_sheet['Signal Group']\n",
    "Rd_E_SigGrop_list = pd_Signal_RD_sheet['EnumName']\n",
    "Rd_SigGrop_notification_list = pd_Signal_RD_sheet['Notification']\n",
    "Rd_SigGrop_tout_list = pd_Signal_RD_sheet['Timeout Notification']\n",
    "Rd_SigGrop_error_factor_list = pd_Signal_RD_sheet['error_factor']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "Rd_Notification_status_t = 'CanMsgSaf_Notification_Status_t;'\n",
    "Rd_fun_ret_type = 'CanMsgSaf_PRI void '\n",
    "Rd_fun_prefix = 'App_CanMsgSaf_Rd_'\n",
    "Rd_fun_name_list = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "for Rd_SigGrop in Rd_SigGrop_list:\n",
    "    if(Rd_SigGrop != 'END'):\n",
    "        Rd_fun_name_list.append(Rd_fun_ret_type + Rd_fun_prefix + Rd_SigGrop + fun_input_type)\n",
    "    else:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_Rd_file_head(file_path):\n",
    "    print(\"/***********************************************************************\", file=file_path)\n",
    "    print(\"Department : IDA-SW2\", file=file_path)\n",
    "    print(\"Time :\" + tm.strftime('%Y-%m-%d %H:%M:%S',tm.localtime(tm.time())) , file=file_path)\n",
    "    print(\"************************************************************************\", file=file_path)\n",
    "    print(\"* Object        : Appl_CanMsgSaf_Rd.c\",file=file_path)\n",
    "    print(\"* Module        : CanMsgSaf_Rd SWC\",file=file_path)\n",
    "    print(\"* Instance      : \",file=file_path)\n",
    "    print(\"* Description   : Can msg function safety\",file=file_path) \n",
    "    print(\"*-----------------------------------------------------------------------*/\",file=file_path)\n",
    "    print(\"\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_Rd_file_Includes(file_path):\n",
    "    print(\"/*=====================Includes=======================================*/\",file=file_path)\n",
    "    print(\"#include \\\"Appl_CanMsgSaf_Rd.h\\\" \", file=file_path)\n",
    "#     print(\"#include \\\"Rte_CtApCanMsgSaf.h\\\" \", file=file_path)\n",
    "    print(\"#include \\\"app_if.h\\\" \", file=file_path)\n",
    "    print(\"\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_Rd_function_describe(fun_name, signal_group_name, swrs_name, swrs_id,  file_path):\n",
    "    print(\"/***********************************************************************\",file=file_path)\n",
    "    print(\"* FUNCTION    :\" + fun_name.replace('CanMsgSaf_PRI',''), file=file_path)\n",
    "    print(\"* DESCRIPTION :\" + \" SigGroup\" + signal_group_name +': ' + swrs_name ,file=file_path)\n",
    "    print(\"* SWRS-ID     : \" + swrs_id.replace('\\n',','), file=file_path)\n",
    "    print(\"* INPUTS      : From RTE\", file=file_path)\n",
    "    print(\"* RETURN      : None\", file=file_path)\n",
    "    print(\"***********************************************************************/\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_Rd_file_Macros(file_path):\n",
    "    print(\"/*=====================Macros=========================================*/\\n\",file=file_path)\n",
    "    print(\"#define CanMsgSaf_RD_NoError                0x00\",file=file_path)\n",
    "    print(\"#define CanMsgSaf_RD_HaveError              0x01\",file=file_path)\n",
    "    print(\"# define RD_STD_OFF                         0u\",file=file_path)\n",
    "    print(\"# define RD_STD_ON                          1u\",file=file_path)\n",
    "    print(\"#define CanMsgSaf_Rd_DEBUG                  RD_STD_OFF\",file=file_path)\n",
    "    print(\"\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_Rd_file_Variables(file_path):\n",
    "    print(\"/*=====================Local Variables================================*/\",file=file_path)\n",
    "    print(\"CanMsgSaf_Rd_SigGroupInfo_t CanMsgSaf_Rd_SigGroupInfo[ SigGroupAll ];\\n\",file=file_path)\n",
    "    print(Rd_Notification_status_t.replace(';', ' ') + Rd_Notification_status_t[:-3] + \";\\n\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_Rd_function_var(signal_group_name, file_path):\n",
    "    print('\\t' + 'unsigned char CanMsgSaf_recover = CanMsgSaf_RD_NoError;',file=file_path)\n",
    "    print(\"\",file=file_path)\n",
    "    print('\\t' + 'Idt' + signal_group_name + ' ' + signal_group_name.replace('_','') + ';',file=file_path)\n",
    "    print(\"\\n\",file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_Rd_function_notification(notification_list, signal_group_name, file_path):\n",
    "    i = 0\n",
    "    for f_notification in notification_list:\n",
    "        if(f_notification == 'END'):\n",
    "            break\n",
    "        else:\n",
    "            print('void ' + f_notification +'()',file = file_path)\n",
    "            print('{',file = file_path)\n",
    "            print('\\t' + Rd_Notification_status_t[:-3] + '.' + signal_group_name.at[i] + ' = CanMsgSaf_RD_NoError;',file = file_path)\n",
    "            print('}',file = file_path)\n",
    "            print(' ',file = file_path)\n",
    "            i = i + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_Rd_function_tout(tout_list, signal_group_name, file_path):\n",
    "    i = 0\n",
    "    for f_tout in tout_list:\n",
    "        if(f_tout == 'END'):\n",
    "            break\n",
    "        else:\n",
    "            print('void ' + f_tout +'()',file = file_path)\n",
    "            print('{',file = file_path)\n",
    "            print('\\t' + Rd_Notification_status_t[:-3]+ '.' + signal_group_name.at[i] + ' = CanMsgSaf_RD_HaveError;',file = file_path)\n",
    "            print('}',file = file_path)\n",
    "            print(' ',file = file_path)\n",
    "            i = i + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_Rd_function_Process():\n",
    "    print(\"/***********************************************************************\",file=file_path)\n",
    "    print(\"* FUNCTION    :\" + 'void ' + \"Appl_Rd_CanMsgSafProcess\", file=file_path)\n",
    "    print(\"* DESCRIPTION :\" + \"Appl_Rd_CanMsgSafProcess\"  ,file=file_path)\n",
    "    print(\"* INPUTS      : None\", file=file_path)\n",
    "    print(\"* RETURN      : None\", file=file_path)\n",
    "    print(\"***********************************************************************/\",file=file_path)\n",
    "    print( 'void ' + \"Appl_Rd_CanMsgSafProcess(void)\", file=file_path)\n",
    "    print('{',file=file_path)\n",
    "    for f_name in Rd_fun_name_list:\n",
    "        print('\\t'+ f_name.replace(' ', '').replace('CanMsgSaf_PRI', '').replace('void', '') +';' ,file=file_path)\n",
    "    print('}',file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "def define_Rd_functionGetStatus():\n",
    "    print(\"/***********************************************************************\",file=file_path)\n",
    "    print(\"* FUNCTION    :\" + 'void ' + \"Appl_CanMsgSaf_Rd_GetStatus\", file=file_path)\n",
    "    print(\"* DESCRIPTION :\" + \"Appl_CanMsgSaf_Rd_GetStatus\"  ,file=file_path)\n",
    "    print(\"* INPUTS      : None\", file=file_path)\n",
    "    print(\"* RETURN      : None\", file=file_path)\n",
    "    print(\"***********************************************************************/\",file=file_path)\n",
    "    print( 'unsigned char Appl_CanMsgSaf_Rd_GetStatus(CanMsgSaf_Rd_SignalName_t CanMsgSaf_Rd_SignalName)', file=file_path)\n",
    "    print('{',file=file_path)\n",
    "    print('\\t'+ 'unsigned char CanMsgSafFalg = 0;\\n' ,file=file_path)\n",
    "    print('\\t'+ 'if(CanMsgSaf_SignalName <  SigGroupAll)' ,file=file_path)\n",
    "    print('\\t{',file=file_path)\n",
    "    print('\\t\\t'+ 'CanMsgSafFalg =  CanMsgSaf_SigGroupInfo[CanMsgSaf_Rd_SignalName].CanMsgSaf_RD_SigGroupInfo.CanMsgSaf_flag;' ,file=file_path) \n",
    "    print('\\t}',file=file_path)\n",
    "    print('\\t' + 'else',file=file_path)\n",
    "    print('\\t{',file=file_path)\n",
    "    print('\\t\\t' + 'CanMsgSafFalg =  0xff;',file=file_path)\n",
    "    print('\\t}',file=file_path)\n",
    "    print('\\t' +'return CanMsgSafFalg;',file=file_path)\n",
    "    print('}',file=file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 0\n",
    "with open('./Appl_CanMsgSaf_Rd.c',mode='w') as file_path:\n",
    "    define_Rd_file_head(file_path)\n",
    "    define_Rd_file_Includes(file_path)\n",
    "    define_Rd_file_Macros(file_path)\n",
    "    define_Rd_file_Variables(file_path)\n",
    "    define_Rd_function_notification(Rd_SigGrop_notification_list, Rd_SigGrop_list,file_path)\n",
    "    define_Rd_function_tout(Rd_SigGrop_tout_list, Rd_SigGrop_list,file_path)\n",
    "    for Rd_fun_name in Rd_fun_name_list:\n",
    "        define_Rd_function_describe(Rd_fun_name,'','','',file_path)\n",
    "        print(Rd_fun_name,file = file_path)\n",
    "        print('{',file = file_path)\n",
    "        define_Rd_function_var('',file_path)\n",
    "        print('\\t' + 'if(' +  Rd_Notification_status_t[:-3] + '.' + Rd_SigGrop_list.at[i] + ' != ' \n",
    "              + 'CanMsgSaf_RD_NoError)',file = file_path)\n",
    "        print('\\t' +'{',file = file_path)\n",
    "        print('\\t\\t'+ 'CanMsgSaf_recover = CanMsgSaf_RD_Error;',file = file_path)\n",
    "        print('\\t' +'}',file = file_path)\n",
    "        print('\\t'+ 'if('+ 'CanMsgSaf_recover != CanMsgSaf_RD_Error)',file = file_path)\n",
    "        print('\\t' +'{',file = file_path)\n",
    "        print('\\t' +'/*todo: counter process*/',file = file_path)\n",
    "        print('\\t' +'}' ,file = file_path)\n",
    "        print('\\t'+ 'if('+ 'CanMsgSaf_recover == CanMsgSaf_RD_Error)',file = file_path)\n",
    "        print('\\t' +'{',file = file_path)\n",
    "        print('\\t\\t'+ 'if('+ 'CanMsgSaf_Rd_SigGroupInfo['+ Rd_E_SigGrop_list.at[i]+'].CanMsgSaf_count < 0x7f' +')',file = file_path)\n",
    "        print('\\t\\t' +'{',file = file_path)\n",
    "        print('\\t\\t\\t'+ 'CanMsgSaf_Rd_SigGroupInfo['+ Rd_E_SigGrop_list.at[i]+'].CanMsgSaf_count++;',file = file_path)\n",
    "        print('\\t\\t' +'}',file = file_path)\n",
    "        print('\\t\\t'+ 'if('+ 'CanMsgSaf_Rd_SigGroupInfo['+ Rd_E_SigGrop_list.at[i]+'].CanMsgSaf_count == '+ str(Rd_SigGrop_error_factor_list.at[i]) +')',file = file_path)\n",
    "        print('\\t\\t' +'{',file = file_path)\n",
    "        print('\\t\\t\\t'+ 'CanMsgSaf_Rd_SigGroupInfo['+ Rd_E_SigGrop_list.at[i]+'].CanMsgSaf_flag = 1;',file = file_path)\n",
    "        print('\\t\\t' +'}',file = file_path)\n",
    "        print('\\t' +'}' ,file = file_path)\n",
    "        print('\\t' +'else' ,file = file_path)\n",
    "        print('\\t' +'{',file = file_path)\n",
    "        print('\\t\\t'+ 'CanMsgSaf_Rd_SigGroupInfo['+ Rd_E_SigGrop_list.at[i]+'].CanMsgSaf_count = 0;',file = file_path)\n",
    "        print('\\t\\t'+ 'CanMsgSaf_Rd_SigGroupInfo['+ Rd_E_SigGrop_list.at[i]+'].CanMsgSaf_flag = 0;',file = file_path)\n",
    "        print('\\t' +'}',file = file_path)\n",
    "        print('}',file = file_path)\n",
    "        i = i + 1\n",
    "    define_Rd_function_Process()\n",
    "    define_Rd_functionGetStatus()\n",
    "    file_path.close()\n",
    "i = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('./Appl_CanMsgSaf_Rd.h',mode='w') as file_path:\n",
    "    print(\"/***********************************************************************\", file=file_path)\n",
    "    print(\"Department : IDA-SW2\", file=file_path)\n",
    "    print(\"Time :\" + tm.strftime('%Y-%m-%d %H:%M:%S',tm.localtime(tm.time())) , file=file_path)\n",
    "    print(\"************************************************************************\", file=file_path)\n",
    "    print(\"* Object        : Appl_CanMsgSaf.h\",file=file_path)\n",
    "    print(\"* Module        : CanMsgSaf_Rd SWC\",file=file_path)\n",
    "    print(\"* Instance      : \",file=file_path)\n",
    "    print(\"* Description   : Can msg function safety\",file=file_path) \n",
    "    print(\"*-----------------------------------------------------------------------*/\",file=file_path)\n",
    "    print(\"#ifndef Appl_CanMsgSaf_Rd_H\",file=file_path)\n",
    "    print(\"#define Appl_CanMsgSaf_Rd_H\",file=file_path)\n",
    "    print(\"/*=====================Includes=======================================*/\",file=file_path)\n",
    "    print(\"\",file=file_path)\n",
    "    print(\"/*=====================Macros=========================================*/\",file=file_path)\n",
    "    print(\"#define CanMsgSaf_PRI static\",file=file_path)\n",
    "    print(\"/*=====================Local Variables================================*/\",file=file_path)\n",
    "    print(\"typedef struct {\",file=file_path)\n",
    "    for Rd_SigG in Rd_SigGrop_list:\n",
    "        if(Rd_SigG != 'END'):\n",
    "            print(\"\\t unsigned char \" +  Rd_SigG + \":1;\",file=file_path)\n",
    "        else:\n",
    "            break\n",
    "    print(\"}\" + Rd_Notification_status_t + \"\\n\",file=file_path)\n",
    "    print(\"typedef struct {\",file=file_path)\n",
    "    print(\"\\t unsigned char CanMsgSaf_flag:1;\",file=file_path)\n",
    "    print(\"\\t unsigned char CanMsgSaf_count:7;\",file=file_path)\n",
    "    print(\"}CanMsgSaf_RD_SigGroupInfo_t;\\n\",file=file_path)\n",
    "\n",
    "\n",
    "    print(\"typedef struct {\",file=file_path)\n",
    "    print(\"\\t CanMsgSaf_RD_SigGroupInfo_t CanMsgSaf_RD_SigGroupInfo;\",file=file_path)\n",
    "    print(\"\\t CanMsgSaf_CanMsgSafEntry_t CanMsgSaf_CanMsgSafEntry;\",file=file_path)\n",
    "    print(\"}CanMsgSaf_Rd_SigGroupInfo_t;\\n\",file=file_path)\n",
    "\n",
    "    i = 0\n",
    "    print(\"typedef enum {\", file=file_path)\n",
    "    while(Rd_E_SigGrop_list.at[i] != 'END'):\n",
    "        print('\\t' + Rd_E_SigGrop_list.at[i] + \" = \" + str(i) +',', file=file_path)\n",
    "        i = i + 1\n",
    "    print(\"\\tSigGroupAll\", file=file_path) \n",
    "    print(\"}CanMsgSaf_Rd_SignalName_t;\\n\", file=file_path) \n",
    "    i = 0\n",
    "\n",
    "    # for f_name in function_name_extern:\n",
    "    #     print(\"extern void \" + f_name +';' ,file=file_path)\n",
    "    # print( 'extern void ' + \"Appl_CanMsgSaf_Init(void);\", file=file_path)  \n",
    "    print( 'extern void ' + \"Appl_CanMsgSaf_Rd_Process(void);\", file=file_path)\n",
    "    print( 'extern unsigned char ' + \"Appl_CanMsgSaf_Rd_GetStatus(CanMsgSaf_Rd_SignalName_t CanMsgSaf_Rd_SignalName);\", file=file_path)\n",
    "#     print( 'extern void ' + \"Appl_CanMsgSafEntry(void);\", file=file_path)\n",
    "    print( '\\n\\n\\n', file=file_path)\n",
    "    print( '#endif /* Appl_CanMsgSaf_Rd_H */', file=file_path)\n",
    "    file_path.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "执行成功。。。。\n"
     ]
    }
   ],
   "source": [
    "print('执行成功。。。。')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
